﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using LDSP.BRL;

namespace LDSP.UI
{
    public partial class Login : BRL.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindDDLEnterprise();
            }
        }

        protected void BindDDLEnterprise()
        {
            DataTable dt = new Enterprise().GetAll();
            this.ddl_enterprise.Items.Clear();
            this.ddl_enterprise.Items.Add(new ListItem("系统管理员", "0"));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddl_enterprise.Items.Add(new ListItem(dt.Rows[i]["name"].ToString(), dt.Rows[i]["id"].ToString()));
            }
            this.ddl_enterprise.SelectedIndex = 0;
        }

        public override void RedirectBefore()
        {
            //Note：本页面不需要进行页面跳转判断
        }

        protected void btn_ok_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.Login(this.txt_name.Text.Trim(), this.txt_pwd.Text, this.txt_validate.Text, this.GetInt(this.ddl_enterprise.SelectedValue)))
                {
                    string redirectUrl = string.Empty;

                    if (LoginAdmin != null)
                    {
                        redirectUrl = "~/UI/Admin/Index.aspx";
                    }
                    else
                    {
                        if (LoginUser != null && LoginUser.UserType == EnterpriseUserType.企业管理员)
                        {
                            redirectUrl = "~/UI/EnterpriseAdmin/Index.aspx";
                        }
                        else
                        {
                            redirectUrl = "~/UI/EnterpriseUser/Index.aspx";
                        }
                    }

                    this.Response.Redirect(redirectUrl);
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
    }
}